package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HoOccupancyInfo;
import com.ruoyi.system.service.IHoOccupancyInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 入住信息Controller
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/system/occupancyInfo")
public class HoOccupancyInfoController extends BaseController {
    @Autowired
    private IHoOccupancyInfoService hoOccupancyInfoService;

    /**
     * 查询入住信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:occupancyInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(HoOccupancyInfo hoOccupancyInfo) {
        startPage();
        List<HoOccupancyInfo> list = hoOccupancyInfoService.selectHoOccupancyInfoList(hoOccupancyInfo);
        return getDataTable(list);
    }

    /**
     * 导出入住信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:occupancyInfo:export')")
    @Log(title = "入住信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoOccupancyInfo hoOccupancyInfo) {
        List<HoOccupancyInfo> list = hoOccupancyInfoService.selectHoOccupancyInfoList(hoOccupancyInfo);
        ExcelUtil<HoOccupancyInfo> util = new ExcelUtil<HoOccupancyInfo>(HoOccupancyInfo.class);
        util.exportExcel(response, list, "入住信息数据");
    }

    /**
     * 获取入住信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:occupancyInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(hoOccupancyInfoService.selectHoOccupancyInfoById(id));
    }

    /**
     * 入住登记
     */
    @PreAuthorize("@ss.hasPermi('system:occupancyInfo:add')")
    @Log(title = "入住登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HoOccupancyInfo hoOccupancyInfo) {
        // 校验是否已经办理了入住
        HoOccupancyInfo param = new HoOccupancyInfo();
        param.setStudentNum(hoOccupancyInfo.getStudentNum());
        param.setStatus("1");
        List<HoOccupancyInfo> hoOccupancyInfos = hoOccupancyInfoService.selectHoOccupancyInfoList(param);
        if (!CollectionUtils.isEmpty(hoOccupancyInfos)) {
            throw new RuntimeException(hoOccupancyInfo.getName() + "|" + hoOccupancyInfo.getStudentNum() + " 已经办理入住");
        }
        // 办理入住
        hoOccupancyInfo.setCheckInOfficer(getUserId());
        hoOccupancyInfo.setStatus("1");
        hoOccupancyInfo.setCreateUserId(getUserId());
        return toAjax(hoOccupancyInfoService.insertHoOccupancyInfo(hoOccupancyInfo));
    }

    /**
     * 房间调整
     */
    @PreAuthorize("@ss.hasPermi('system:occupancyInfo:edit')")
    @Log(title = "房间调整", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HoOccupancyInfo hoOccupancyInfo) {
        HoOccupancyInfo old = hoOccupancyInfoService.selectHoOccupancyInfoById(hoOccupancyInfo.getId());
        if (!"1".equals(old.getStatus())) {
            throw new RuntimeException(hoOccupancyInfo.getName() + "|" + hoOccupancyInfo.getStudentNum() + " 当前状态不是在宿状态，不能调整房间!");
        }
        hoOccupancyInfoService.changeApplication(old, hoOccupancyInfo.getDormitoryId(), getUserId(), hoOccupancyInfo.getApplicationInfo(), hoOccupancyInfo.getCheckOutTime());
        return toAjax(1);
    }

    /**
     * 删除入住信息
     */
    @PreAuthorize("@ss.hasPermi('system:occupancyInfo:remove')")
    @Log(title = "入住信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hoOccupancyInfoService.deleteHoOccupancyInfoByIds(ids));
    }
}
